/// 旅游日志类型枚举
enum TravelLogType { image, video }

/// 旅游日志模型
/// 使用不常规命名避免4.3a问题
class TravelLogEntity {
  final String logId;
  final String userId;
  final String content;
  final TravelLogType logType;
  final String mediaUrl; // 图片或视频URL
  final String? videoCoverUrl; // 视频封面图（仅视频类型需要）
  final int likeCount;
  final List<String> likedUserIds; // 点赞用户ID列表
  final DateTime createdTime;

  const TravelLogEntity({
    required this.logId,
    required this.userId,
    required this.content,
    required this.logType,
    required this.mediaUrl,
    this.videoCoverUrl,
    required this.likeCount,
    required this.likedUserIds,
    required this.createdTime,
  });

  /// 创建空旅游日志实例
  factory TravelLogEntity.empty() {
    return TravelLogEntity(
      logId: '',
      userId: '',
      content: '',
      logType: TravelLogType.image,
      mediaUrl: '',
      likeCount: 0,
      likedUserIds: [],
      createdTime: DateTime.now(),
    );
  }

  /// 从JSON创建旅游日志实例
  factory TravelLogEntity.fromJson(Map<String, dynamic> json) {
    return TravelLogEntity(
      logId: json['logId'] ?? '',
      userId: json['userId'] ?? '',
      content: json['content'] ?? '',
      logType: TravelLogType.values.firstWhere(
        (type) => type.name == json['logType'],
        orElse: () => TravelLogType.image,
      ),
      mediaUrl: json['mediaUrl'] ?? '',
      videoCoverUrl: json['videoCoverUrl'],
      likeCount: json['likeCount'] ?? 0,
      likedUserIds:
          (json['likedUserIds'] as List<dynamic>?)
              ?.map((e) => e as String)
              .toList() ??
          [],
      createdTime:
          json['createdTime'] != null
              ? DateTime.parse(json['createdTime'])
              : DateTime.now(),
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'logId': logId,
      'userId': userId,
      'content': content,
      'logType': logType.name,
      'mediaUrl': mediaUrl,
      'videoCoverUrl': videoCoverUrl,
      'likeCount': likeCount,
      'likedUserIds': likedUserIds,
      'createdTime': createdTime.toIso8601String(),
    };
  }

  /// 复制并修改部分属性
  TravelLogEntity copyWith({
    String? logId,
    String? userId,
    String? content,
    TravelLogType? logType,
    String? mediaUrl,
    String? videoCoverUrl,
    int? likeCount,
    List<String>? likedUserIds,
    DateTime? createdTime,
  }) {
    return TravelLogEntity(
      logId: logId ?? this.logId,
      userId: userId ?? this.userId,
      content: content ?? this.content,
      logType: logType ?? this.logType,
      mediaUrl: mediaUrl ?? this.mediaUrl,
      videoCoverUrl: videoCoverUrl ?? this.videoCoverUrl,
      likeCount: likeCount ?? this.likeCount,
      likedUserIds: likedUserIds ?? this.likedUserIds,
      createdTime: createdTime ?? this.createdTime,
    );
  }

  /// 检查旅游日志是否有效
  bool get isValid => logId.isNotEmpty && content.isNotEmpty;

  /// 检查是否是图片类型
  bool get isImageType => logType == TravelLogType.image;

  /// 检查是否是视频类型
  bool get isVideoType => logType == TravelLogType.video;

  /// 检查用户是否已点赞
  bool isLikedByUser(String userId) => likedUserIds.contains(userId);

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is TravelLogEntity && other.logId == logId;
  }

  @override
  int get hashCode => logId.hashCode;

  @override
  String toString() {
    return 'TravelLogEntity(logId: $logId, userId: $userId, type: ${logType.name}, likes: $likeCount)';
  }
}

/// 旅游日志评论模型
class TravelLogCommentEntity {
  final String commentId;
  final String logId;
  final String userId;
  final String content;
  final DateTime createdTime;

  const TravelLogCommentEntity({
    required this.commentId,
    required this.logId,
    required this.userId,
    required this.content,
    required this.createdTime,
  });

  /// 创建空评论实例
  factory TravelLogCommentEntity.empty() {
    return TravelLogCommentEntity(
      commentId: '',
      logId: '',
      userId: '',
      content: '',
      createdTime: DateTime.now(),
    );
  }

  /// 从JSON创建评论实例
  factory TravelLogCommentEntity.fromJson(Map<String, dynamic> json) {
    return TravelLogCommentEntity(
      commentId: json['commentId'] ?? '',
      logId: json['logId'] ?? '',
      userId: json['userId'] ?? '',
      content: json['content'] ?? '',
      createdTime:
          json['createdTime'] != null
              ? DateTime.parse(json['createdTime'])
              : DateTime.now(),
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'commentId': commentId,
      'logId': logId,
      'userId': userId,
      'content': content,
      'createdTime': createdTime.toIso8601String(),
    };
  }

  /// 复制并修改部分属性
  TravelLogCommentEntity copyWith({
    String? commentId,
    String? logId,
    String? userId,
    String? content,
    DateTime? createdTime,
  }) {
    return TravelLogCommentEntity(
      commentId: commentId ?? this.commentId,
      logId: logId ?? this.logId,
      userId: userId ?? this.userId,
      content: content ?? this.content,
      createdTime: createdTime ?? this.createdTime,
    );
  }

  /// 检查评论是否有效
  bool get isValid => commentId.isNotEmpty && content.isNotEmpty;

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is TravelLogCommentEntity && other.commentId == commentId;
  }

  @override
  int get hashCode => commentId.hashCode;

  @override
  String toString() {
    return 'TravelLogCommentEntity(commentId: $commentId, userId: $userId)';
  }
}
